#!/usr/bin/env zsh

emulate -L zsh -o err_return -o no_unset -o warn_create_global -o pipe_fail

() {

(( ARGC == 2 ))

() {
  local cmd
  for cmd in rm mkdir xargs git; do
    if [[ ! -v commands[$cmd] ]]; then
      print -ru2 -- "${ZSH_SCRIPT:t}: command not found: ${(q-)cmd}"
      return 1
    fi
  done
}

local dir=$1
local kind=$2

command rm -rf -- $dir
command mkdir -p -- $dir
cd -q -- $dir

local -x GIT_CONFIG_NOSYSTEM=1
local err
err=$(
  exec 2>&1 >/dev/null
  command git init
  if [[ $kind == yes ]]; then
    print -rC1 -- {1..10}/{1..10}/{1..10}         | command xargs mkdir -p --
    print -rC1 -- {1..10}/{1..10}/{1..10}/{1..10} | command xargs touch --
    zmodload zsh/zselect
    zselect -t 100 || true
    command git add .
    command git commit --quiet --allow-empty --allow-empty-message --no-gpg-sign -m ''
    command git status
  fi
) || {
  print -ru2 -- $err
  return 1
}

} "$@"
